﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web;
using System.IO;

namespace STAR.Spring.Framework.Module
{
    /// <summary>
    /// （多请求参数）针对同时多请求
    /// </summary>
    public class RequestJsonParmInfos
    {
        public bool IsParmRight { get; set; }

        public RequestJsonParmInfos_Item[] Items { get; set; }

        private RequestJsonParmInfos() { }

        public static RequestJsonParmInfos GetRequestJsonParmInfos()
        {
            RequestJsonParmInfos info = new RequestJsonParmInfos();
            info.IsParmRight = false;
            info.Items = RequestJsonParmInfos_Item.GetItems();
            if (info.Items != null)
            {
                info.IsParmRight = true;
            }
            return info;
        }

        public override string ToString()
        {
            StringBuilder builder=new StringBuilder();
            if (Items!=null)
            {
                for (int index = 0; index < Items.Length; index++)
                {
                    builder.AppendFormat("{0}、{1} {2} {3}", Items[index].RequestName, Items[index].Op, Items[index].IsCustom);
                    if (Items[index].Parms==null)
                    {
                        continue;
                    }
                    builder.Append("\r\n参数开始：\r\n");
                    foreach (string param in Items[index].Parms)
                    {
                        builder.AppendFormat(" {0}", param);
                    }
                    builder.Append("\r\n参数完成\r\n");
                }
            }
            return builder.ToString();
        }
    }
    public class RequestJsonParmInfos_Item
    {
        public string RequestName { get; set; }

        public string[] Parms { get; set; }

        public string Op { get; set; }

        public static RequestJsonParmInfos_Item[] GetItems()
        {
            RequestJsonParmInfos_Item[] obj = null;
            HttpRequest Request = HttpContext.Current.Request;
            StreamReader sr = new StreamReader(Request.InputStream);
            string jsonString = sr.ReadToEnd();
            try
            {
                obj = new JavaScriptSerializer().Deserialize<RequestJsonParmInfos_Item[]>(jsonString);

            }
            catch
            {

            }

            return obj;
        }

        public bool IsCustom { get; set; }
    }
}
